//
//  WorkViewController.m
//  掌上海印
//
//  Created by 陈德锋 on 16/4/17.
//  Copyright © 2016年 handsome. All rights reserved.
//

#import "WorkViewController.h"
#import "NoteCell.h"
#import "MemorandumModel.h"
#import "BQLModelTool.h"
#import "AddNote.h"
#import "EditNote.h"
#import "UITableView+EmptyData.h"

@interface WorkViewController () <UITableViewDataSource,UITableViewDelegate>
{
    UITableView *MainTableView; // 主表格
}

@property (nonatomic, strong) NSMutableArray *modalsArrM;
@property (nonatomic, strong) FMDatabase *fmdb;

@end

@implementation WorkViewController

- (NSMutableArray *)modalsArrM {
    if (!_modalsArrM) {
        _modalsArrM = [[NSMutableArray alloc] init];
    }
    return _modalsArrM;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self initNoteData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"工作任务";
    UIBarButtonItem *addBtn=[[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(AddClick)]; // 新建按钮
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 
    self.navigationItem.rightBarButtonItem=addBtn;
    
    [self setMainTableView];
}

- (void)setMainTableView
{
    MainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
    MainTableView.dataSource = self;
    MainTableView.delegate = self;
    [self.view addSubview:MainTableView];
}

// 查询工作任务(初始化
- (void)initNoteData
{
    [self.modalsArrM removeAllObjects];
    NSArray *modals = [BQLModelTool queryData:nil];
    NSArray *tempArr = [[modals reverseObjectEnumerator] allObjects]; // 将数组倒序排列
    [self.modalsArrM addObjectsFromArray:tempArr];
    [MainTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [tableView tableViewDisplayWitMsg:@"您还没有写工作任务" ifNecessaryForRowCount:self.modalsArrM.count];
    return self.modalsArrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identfiy = @"cellID";
    NoteCell *cell = [tableView dequeueReusableCellWithIdentifier:identfiy];
    if(!cell){
        cell = [[NoteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfiy];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setNoteData:self.modalsArrM[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditNote *edit = [[EditNote alloc] init];
    edit.model = self.modalsArrM[indexPath.row];
    [self.navigationController pushViewController:edit animated:YES];
}

#pragma cell编辑模式--------删除工作任务
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){ //删除模式
        [BQLModelTool deleteData:self.modalsArrM[indexPath.row]];
        //[MainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [self initNoteData]; // 重新查询数据刷新表格
}


#pragma  自定义左滑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25 * [[UIScreen mainScreen]bounds].size.height/667.0 + 34.5;
}

// 新建工作任务
- (void)AddClick
{
    AddNote *add = [[AddNote alloc] init];
    [self.navigationController pushViewController:add animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
